package property.personal.property_mange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import property.personal.property_mange.dao.PropertyMapper;
import property.personal.property_mange.pojo.Propertyinfo;
import property.personal.property_mange.service.ProperService;


@Service
public class ProperImpl implements ProperService {
    @Autowired
    PropertyMapper propertyMapper;

    @Override
    public int addproperty(Propertyinfo propertyInfo) {
        return propertyMapper.insert(propertyInfo);
    }

    @Override
    public IPage getpagedata(int page, int limit) {
        Page<Propertyinfo> pages = new Page<>(page,limit);
        QueryWrapper<Propertyinfo> wrapper = new QueryWrapper<>();
        IPage ipage= propertyMapper.selectPage(pages,wrapper);    // 查询
        ipage.getRecords().forEach(System.out::println); // 获取内容
        return ipage;
    }

    @Override
    public IPage getpagedatasearch(int page, int limit, String propertyname) {
        Page<Propertyinfo> pages = new Page<>(page,limit);
        QueryWrapper<Propertyinfo> wrapper = new QueryWrapper<>();
        wrapper.like("propertyname",propertyname);
        IPage ipage= propertyMapper.selectPage(pages,wrapper);    // 查询
        ipage.getRecords().forEach(System.out::println); // 获取内容
        return ipage;
    }

    @Override
    public int updatebyid(String id,String status,int code) {
        Propertyinfo propertyinfo = new Propertyinfo();
        propertyinfo.setId(id);
        propertyinfo.setStatus(status);
        propertyinfo.setPropertycode(code);
        System.out.println("code="+propertyinfo.getPropertycode());
        int i = propertyMapper.updateById(propertyinfo);
        return i;
    }

    @Override
    public IPage asc(int page, int limit, String type) {
        Page<Propertyinfo> pages = new Page<>(page,limit);
        QueryWrapper<Propertyinfo> wrapper = new QueryWrapper<>();
        if ("asc".equals(type)){
            wrapper.orderByAsc("id");
        }else{
            wrapper.orderByDesc("id");
        }
        IPage ipage= propertyMapper.selectPage(pages,wrapper);    // 查询
        ipage.getRecords().forEach(System.out::println); // 获取内容
        return ipage;
    }

    @Override
    public IPage ascsearch(int page, int limit, String type, String propertyname) {
        Page<Propertyinfo> pages = new Page<>(page,limit);
        QueryWrapper<Propertyinfo> wrapper = new QueryWrapper<>();
        if ("asc".equals(type)){
            wrapper.orderByAsc("id");
        }else{
            wrapper.orderByDesc("id");
        }
        wrapper.like("propertyname",propertyname);
        IPage ipage= propertyMapper.selectPage(pages,wrapper);    // 查询
        ipage.getRecords().forEach(System.out::println); // 获取内容
        return ipage;
    }

    @Override
    public Propertyinfo getedit(String id) {
        Propertyinfo propertyinfo = propertyMapper.selectById(id);
        return propertyinfo;
    }

    @Override
    public int updateby(String id, String columname, String value) {
        Propertyinfo propertyinfo = new Propertyinfo();
        System.out.println("columname=="+columname);
        propertyinfo.setId(id);
        if ("propertyname".equals(columname)){
            propertyinfo.setPropertyname(value);
        }else if("propertycode".equals(columname)){
            int code=Integer.parseInt(value);
            propertyinfo.setPropertycode(code);
        }else if("director".equals(columname)){
            propertyinfo.setDirector(value);
        }else if("telphone".equals(columname)){
            propertyinfo.setTelphone(value);
        }else if("propertyaddress".equals(columname)){
            propertyinfo.setPropertyaddress(value);
        }else if("propertyindroduce".equals(columname)){
            propertyinfo.setPropertyindroduce(value);
        }
        int i = propertyMapper.updateById(propertyinfo);
        return i;
    }

}
